<?php
//查询配置
include_once("../../untils/conn.php");

$sql = "SELECT * FROM kefu";
$result = mysqli_query($con, $sql);
if ($result) {
  $row = mysqli_fetch_assoc($result);
  $appid = $row['appid'];
  $key = $row['key'];
}

// 获取表单数据
$user_id = $appid;
$timestamp = time();
$name = $_POST['name'];
$phone = $_POST['phone'];
$idcard = $_POST['uid'];
$province = $_POST['sf'];
$city = $_POST['city'];
$area = $_POST['xian'];
$address = $_POST['dizhi'];
$product_id = $_POST['haokaid'];
$down_order_id = $_POST['oderid'];
$third_phone = "";
$secret = $key;

// 计算签名
$user_sign = md5(
    "Address=" . $address .
    "&Area=" . $area .
    "&City=" . $city .
    "&DownOrderID=" . $down_order_id .
    "&IDCard=" . $idcard .
    "&Name=" . $name .
    "&Phone=" . $phone .
    "&ProductID=" . $product_id .
    "&Province=" . $province .
    "&ThirdPhone=" . $third_phone .
    "&Timestamp=" . $timestamp .
    "&user_id=" . $user_id .
    $secret);

// 构造请求参数
$post_data = array(
    'user_id' => $user_id,
    'Timestamp' => $timestamp,
    'Name' => $name,
    'Phone' => $phone,
    'IDCard' => $idcard,
    'Province' => $province,
    'City' => $city,
    'Area' => $area,
    'Address' => $address,
    'ProductID' => $product_id,
    'DownOrderID' => $down_order_id,
    'ThirdPhone' => $third_phone,
    'user_sign' => $user_sign.$secret
);

$json_data = json_encode($post_data);
echo $json_data;


// 提交请求
// $url = "https://haokaopenapi.lot-ml.com/api/order/ApiToOrder";
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// $output = curl_exec($ch);
// curl_close($ch);

// 解析返回结果
$result = json_decode($output, true);
if ($result['code'] == 0) {
    echo "下单成功，订单号：" . $result['message'];
} else {
    echo "下单失败，错误信息：" . $result['message'];
}
?>
